package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.common.pojo.ResponseResult;
import com.lg.product.entity.Category;
import com.lg.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/category")
public class CategoryController {




    @Autowired
    ICategoryService iCategoryService;

    @RequestMapping("/list")
    public ResponseResult list(){
        List<Category> categories = iCategoryService.selectByParenId(0L);
        return ResponseResult.success(categories);
    }

    @RequestMapping("/findSan")
    public ResponseResult findSan(Long id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id",id);
        List list = iCategoryService.list(wrapper);
        return ResponseResult.success(list);
    }


    @RequestMapping("/insert")
    public ResponseResult insert(@RequestBody Category category){
        Random random = new Random();
        category.setSort(random.nextInt(9)+1);
        int insert = iCategoryService.insert(category);
        return ResponseResult.success(insert);
    }

    @RequestMapping("/delete")
    public ResponseResult delete(@RequestBody Category category){


        return ResponseResult.success(iCategoryService.deletes(category.getId()));
    }


    @RequestMapping("/update")
    public ResponseResult update(@RequestBody Category category){
        boolean update = iCategoryService.updateById(category);
        return ResponseResult.success(update);
    }


}
